package com.licenta.fbApp.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwittit.client.facebook.ConnectState;
import com.gwittit.client.facebook.FacebookConnect;
import com.gwittit.client.facebook.LoginCallback;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class FBon implements EntryPoint {
	
	/**
	 * @uml.property  name="connected"
	 */
	private boolean connected = false;
	/**
	 * @uml.property  name="isUsing"
	 */
	private boolean isUsing = true;
	  /**
	   * This is called when user logs in.
	   */
	  public void loadApp() {
	    String token = History.getToken();
	    String key = "uid=";
	    Long uid;
	    if (token.lastIndexOf(key) != -1) {
	      uid = Long.parseLong(token.substring(token.lastIndexOf(key) + key.length()));
	    } else {
	      uid = DisplayUserInfo.getUid();
	    }
	    RootLayoutPanel.get().clear();
	    RootLayoutPanel.get().add(new AppPage(uid, isUsing));
	  }

	public static final String API_KEY = "a3396bd8e4cd8151d627cdb48ab94bee";
	/**
	 * @uml.property  name="myLoginCallback"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private final LoginCallback myLoginCallback = new MyLoginCallback();
	/**
	 * @uml.property  name="contentPanel"
	 * @uml.associationEnd  multiplicity="(1 1)"
	 */
	private final VerticalPanel contentPanel = new VerticalPanel();

	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {
		
		FacebookConnect.init(API_KEY, "/xd_receiver.htm", myLoginCallback);
	    FacebookConnect.waitUntilStatusReady(new RenderAppWhenReadyCallback());
	    RootLayoutPanel.get().add(contentPanel);
System.out.print(connected);
	    History.addValueChangeHandler(new ValueChangeHandler<String>() {
	      
	      public void onValueChange(ValueChangeEvent<String> event) {
	        if (connected) {
	          loadApp();
	        }
	      }
	    });
	    History.fireCurrentHistoryState();

	    AppEventBus.getEventBus().addHandler(FriendClickedEvent.TYPE,
	      new FriendClickedEventHandler() {
	        
	        public void onFriendClicked(FriendClickedEvent friendClickedEvent) {
	          isUsing = friendClickedEvent.isUsing();
	          History.newItem("uid=" + friendClickedEvent.getUid());
	        }
	      });
	}
	
	/**
	   * @author iuliab
	   * 
	   */
	  private class MyLoginCallback implements LoginCallback {
	    
	    public void onLogin() {
	      renderWhenConnected();
	    }
	  }

	  /**
	   * Fired when we know users status
	   */
	  private class RenderAppWhenReadyCallback implements AsyncCallback<ConnectState> {
	    public void onFailure(Throwable caught) {
	      Window.alert("Failed to get status:" + caught);
	      System.out.println("connect failed: " + caught);
	    }

	    public void onSuccess(ConnectState result) {
	    	System.out.println("connect succeded: " + result);
	      if (result == ConnectState.connected) {
	        connected = true;
	        renderWhenConnected();
	      } else {
	        renderWhenNotConnected();
	      }
	    }
	  };

	  /**
	   * Render when user is connected
	   */
	  private void renderWhenConnected() {
	    loadApp();
	  }

	  /**
	   * Render when user is not connected
	   */
	  private void renderWhenNotConnected() {
	    LoginBox loginBox = new LoginBox();
	    loginBox.addLoginCallback(myLoginCallback);
	    contentPanel.setHeight("0");
	    contentPanel.add(loginBox);
	  }

}
